<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="assets/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
	//页面加载完毕事件.
	$(function(){
		//1.获取商品数据,插入到select标签中.
		$.ajax({
			url : "http://localhost/product/list",
			data : null,
			type : "GET",
            dataType : "json",
			xhrFields:{withCredentials:true},
			success : function(msg){
				if(msg.code == 0){
					//到这说明查询数据成功
					//1.获得具体的json对象数组
					var jsonArray = msg.data;
					//2.遍历,封装数据
					//声明最终结果
					var content = "<option value=''>--请选择商品--</option>";
					for(var i = 0;i < jsonArray.length;i++){
						content += "<option value='"+jsonArray[i].id+"'>"+jsonArray[i].productName+"</option>"
					}
					//3.扔到select标签体内.
					$("#proSelect").html(content);
				}else{
					alert(msg.msg);	
				}
			},
			error : function(msg){
				alert("服务器正在更新,请稍后再试....");
			}
		});
	});
	
	//添加销售记录功能
	function saleAdd(){
		//POST	/InSystem/sale/addSale
		//1.获取用户输入的数据
		var data = $("#saleAddForm").serialize();
		//2.发送ajax请求完成添加.
		$.ajax({
			url : "http://localhost/sale/add",
			data : data,
			type : "POST",
            dataType : "json",
			xhrFields:{withCredentials:true},
			success : function(msg){
				if(msg.code == 0){
					//完成添加.清空用户输入的表单项的值.
					$("#saleAddForm")[0].reset();
					alert("添加销售记录成功!!");
				}else{
					alert(msg.msg);	
				}
			},
			error : function(msg){
				alert("服务器正在更新,请稍后再试....");
			}
		});
	}
	
	
	
	
	
	
	
	
	
	
	
</script>
</head>
<body>
  <div align="center" style="line-height: 66px;">
   <form id="saleAddForm" action=""> 
	<table>
		<tr>
			<td colspan="2"><h3>添加销售:</h3></td>
		</tr>
		<tr>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td>商品名称:</td>
			<td>
				<select name="productId" id="proSelect">
					
				</select>
			</td>
		</tr>
		<tr>
			<td>销售单价:</td>
			<td>
				<input type="text" name="price" />
			</td>
		</tr>
		<tr>
			<td>销售数量:</td>
			<td>
				<input type="text" name="quantity" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="button" onclick="saleAdd()" value="保存" />
			</td>
		</tr>
	</table>
   </form>
  </div>
</body>
</html>